package com.demo.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;

import javax.persistence.*;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDate;

@SuppressWarnings("JpaDataSourceORMInspection")
@Entity
@Data
@Table(name = "ACCOUNT")
public class Account implements Serializable {

    private static final long serialVersionUID = 3573429947087924125L;

    @Id
    @GeneratedValue
    @Column(name = "ACCOUNT_ID", updatable = false)
    private Integer accountId;

    @Column(name = "NAME")
    private String accountName;

    @Column(name = "BALANCE")
    private BigDecimal balance;

    @Column(name = "OPENING_DATE")
    private LocalDate openingDate;

    /*
        OnDeleteAction.CASCADE
        删除customer时，联级删除所有account，这是数据库处理的，与ORM无关。
        cascade=CascadeType.REMOVE级联将由orm（hibernate）处理。
     */
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "CUS_ID", nullable = false)
    @OnDelete(action = OnDeleteAction.CASCADE)
    @JsonIgnore
    private Customer customer;
}
